package com.hw.one.dam2.util;

import com.hw.one.dam2.common.DamConstant;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 跟日期有关的工具类
 *
 * @author admin
 */
public class CalendarUtil {

    /**
     * 获取 年份 所在年的最大周数
     *
     * @param year
     * @return
     */
    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = Calendar.getInstance();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);

        return getWeekOfYear(c.getTime());
    }

    /**
     * 获取当前时间所在年的周数
     *
     * @param date
     * @return
     */
    public static int getWeekOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);

        return c.get(Calendar.WEEK_OF_YEAR);
    }


    /**
     * 格式化日期
     *
     * @param date
     * @return
     */
    public static String getDateFormat(Date date, String format) {
        SimpleDateFormat simpleDate = new SimpleDateFormat(format);
        return simpleDate.format(date);
    }

    /**
     * 根据日期字符串 格式化成日期
     *
     * @param timeStr
     * @param format
     * @return
     * @throws Exception
     */
    public static Date getDateByString(String timeStr, String format) throws Exception {
        SimpleDateFormat simpleDate = new SimpleDateFormat(format);
        return simpleDate.parse(timeStr);
    }

    /**
     * 根据月份 返回汉语月份
     *
     * @param month
     * @return
     */
    public static String getMonthCnByMonth(String month) {
        if ("1".equals(month)) {
            return "一月";
        }
        if ("2".equals(month)) {
            return "二月";
        }
        if ("3".equals(month)) {
            return "三月";
        }
        if ("4".equals(month)) {
            return "四月";
        }
        if ("5".equals(month)) {
            return "五月";
        }
        if ("6".equals(month)) {
            return "六月";
        }
        if ("7".equals(month)) {
            return "七月";
        }
        if ("8".equals(month)) {
            return "八月";
        }
        if ("9".equals(month)) {
            return "九月";
        }
        if ("10".equals(month)) {
            return "十月";
        }
        if ("11".equals(month)) {
            return "十一月";
        }
        if ("12".equals(month)) {
            return "十二月";
        }
        return "";

    }


    /**
     * 获取一个日期的最后一个月的日期
     * 参数 "yyyy-MM"
     * 返回值 "yyyy-MM-dd"
     *
     * @param date
     * @return
     */
    public static String getLastDayOfMonth(String date) {
        try {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat(DamConstant.SDF_YM);
            calendar.setTime(sdf.parse(date));
            int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            return date + "-" + lastDay;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 根据开始时间和结束时间分割时间，返回时间列表
     *
     * @param startTime
     * @param endTime
     * @param type      hour:表示以小时分割，day：表示以天分割
     * @return
     */

     public static List<String> getDatesBetweenTwoDate(String startTime, String endTime, String type) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        int time_increment = Calendar.HOUR_OF_DAY;
        if ("day".equals(type)) {
            time_increment = Calendar.DAY_OF_MONTH;
            sdf = new SimpleDateFormat(DamConstant.SDF_YMD);
        }
        List<String> dateList = new ArrayList<>();
        dateList.add(startTime);

        try {
            calendar.setTime(sdf.parse(startTime));
            while (true) {
                //根据日历的规则，为给定的日历字段添加或减去指定的时间量
                calendar.add(time_increment, 1);
                // 测试此日期是否在指定日期之后
                if (sdf.parse(endTime).after(calendar.getTime())) {
                    dateList.add(sdf.format(calendar.getTime()));
                } else {
                    break;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        dateList.add(endTime);
        return dateList;
    }

    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }

    public static void main(String[] args) {
//        System.out.println(getMaxWeekNumOfYear(2017));
//        System.out.println(getMaxWeekNumOfYear(2016));
//        System.out.println(getMaxWeekNumOfYear(2015));
        List<String> l = getDatesBetweenTwoDate("2017-02-13 02:00:00", "2017-02-16 02:00:00", "day");
        for (String s : l) {
            System.out.println(s);
        }
    }
}
